package http.util;

import java.util.*;

/**
 * @author bali2 on 5/9/2021.
 * @version 1.0
 */
public class ArrayUtil {

    public static <T> T getRandomDataFromArrayList(ArrayList array) {
        if (array.isEmpty()) {
            return null;
        }
        Object[] arr = array.toArray();
        Random random = new Random();
        return (T) array.toArray()[random.nextInt(arr.length)];
    }
    public static <T> T getRandomDataFromSet(Set set) {
        if (set.isEmpty()) {
            return null;
        }
        Object[] arr = set.toArray();
        Random random = new Random();
        return (T) set.toArray()[random.nextInt(arr.length)];
    }
    public static <T> T getRandomDataFromHashMap(HashMap hashMap) {
        if (hashMap.isEmpty()) {
            return null;
        }
        Random random = new Random();
        HashMap newMap = new HashMap();
        String key = (String) hashMap.keySet().toArray()[random.nextInt(hashMap.size())];
        newMap.put(key,hashMap.get(key));
        return (T) newMap;
    }

    public static <T> T getRandomDataFromCollection(Collection collection) {
        if (collection.isEmpty()) {
            return null;
        }
        Object[] arr = collection.toArray();
        Random random = new Random();
        return (T) collection.toArray()[random.nextInt(arr.length)];
    }
}
